package com.szrt.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class PermissionFilter implements Filter {

	@Override
	public void destroy() {
		
		System.out.println("过滤器2，权限过滤 destroy");
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest request=(HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse)resp;
		
		String username=request.getParameter("username");
		
		if(username.equals("刘")){
			chain.doFilter(request,response);
		}else{
			
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("code", "444");
			Gson json=new Gson();
			String jsString = json.toJson(map);
			
			response.getWriter().write(jsString);
			
		}
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
		System.out.println("过滤器2，权限过滤 init");

	}

}
